package com.youmingxing.network;

import android.content.Context;
import android.os.Build;
import android.telephony.TelephonyManager;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.MySSLSocketFactory;
import com.loopj.android.http.RequestParams;
import com.youmingxing.BuildConfig;
import com.youmingxing.data.GlobalDataManager;

import org.apache.http.HttpVersion;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;

import java.security.KeyStore;

public class HttpManager {

    public static AsyncHttpClient client;
    public static String IMEI;

    // --------线上数据地址---
//    public static String BASE_URL = "https://api.youmingxing.com/";
    public static String BASE_URL_notS = "http://api.youmingxing.com/";
    // --------测试数据地址---
    public static String BASE_URL ="https://t1.youmingxing.com/";



    public static void initHttpClient(Context context) {
        if (client == null) {
            client = new AsyncHttpClient(getSchemeRegistry());
        }
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        IMEI = manager.getDeviceId();
    }

    public static SchemeRegistry getSchemeRegistry() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            trustStore.load(null, null);
            SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 10000);
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));
            return registry;
        } catch (Exception e) {
            return null;
        }
    }

    public static RequestParams getBaseParams() {
        RequestParams params = new RequestParams();
        params.put("os", 1);
        params.put("sver", Build.VERSION.RELEASE);
        params.put("ver", BuildConfig.VERSION_NAME);
        params.put("gid", IMEI);
        params.put("channel", BuildConfig.FLAVOR);
        params.put("ua", "1");
        params.put("plat", 1);
        if (!GlobalDataManager.token.equals("")) {
            params.put("token", GlobalDataManager.token);
        }
        return params;
    }
}